# from selenium.webdriver.support import expected_conditions as EC
# from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from utils import common


def camera_photo(driver, is_camera, photo_num):
    if is_camera == 1:
        locator = (By.ID, 'org.xinkb.blackboard.android:id/iv_camera')
        common.wait_clickable(driver, locator)
        driver.find_element_by_id('org.xinkb.blackboard.android:id/iv_camera').click()
        # 打开照相机
        camera(driver)
    else:
        # 选择照片
        choose_photo(driver, photo_num)


def camera(driver):
    pass


def choose_photo(driver, num):
    is_photo = common.is_visible_dri(driver, By.ID, 'org.xinkb.blackboard.android:id/tb_btn')  # 是否有照片
    if not is_photo:
        return 0  # 无照片
    locator = (By.ID, 'org.xinkb.blackboard.android:id/tb_btn')
    common.wait_clickable(driver, locator)
    photos = driver.find_elements_by_id('org.xinkb.blackboard.android:id/tb_btn')
    size = len(photos)
    for photo in photos:
        photo.click()  # 勾选照片（按顺序选中照片）
        num = num - 1
        if num == 0:
            break  # 勾选中需要的照片数就不再勾选照片
    photos = driver.find_elements_by_id('org.xinkb.blackboard.android:id/tb_btn')
    size_1 = len(photos)
    if size < num:
        print('已勾选照片：', photos[size_1-1].text)  # 获取已选中的数量
        return size  # 目前只勾选第一屏的照片，后续优化（滑动页面再勾选）
    else:
        print('已勾选照片：', photos[num-1].text)  # 获取已选中的数量
        return num
